<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Receivepurmat"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.OutpurmatDAO"%>
<%@page import="db.ReceivepurmatDAO" %>
<%@page import="db.Tob" %>
<%@page import="db.TeamDAO" %>
<%@page import="db.Team" %>
<%@page import="db.PurreqDAO" %>
<%@page import="db.Retreq" %>
<%@page import="db.RetreqDAO" %>
<%@page import="db.SupplierDAO" %>
<%@page import="db.User"%>
<%@page import="db.UserDAO"%>
<%@page import="db.Group"%>
<%@page import="db.Purmat"%>
<%@page import="db.PurmatDAO"%>
<%@page import="db.Material"%>
<%@page import="db.MaterialDAO"%>
<%@page import="db.Outpurmat"%>
<%@page import="db.Outreq"%>
<%@page import="db.OutreqDAO"%>
<%@page import="db.Retmat"%>
<%@page import="db.RetmatDAO"%>
<%@page import="db.BorreqDAO"%>
<%@page import="db.Borreq"%>
<%@page import="db.BormatDAO"%>
<%@page import="db.Borreq"%>
<%@page import="db.Bormat"%>
<%@page import="db.BackmatDAO"%>
<%@page import="db.Backmat"%>
<%@page import="db.Backreq"%>
<%@page import="db.BackreqDAO"%>
<%@page import="db.Borreq"%>
<%@page import="db.Bormat"%>
<%@page import="db.BackmatDAO"%>
<%@page import="db.Backmat"%>
<%@page import="db.Backreq"%>
<%@page import="db.BackreqDAO"%>
<%@page import="db.Tomat"%>
<%@page import="db.TomatDAO"%>
<%@page import="db.Toreq"%>
<%@page import="db.ToreqDAO"%>
<%@page import="db.Backmat"%>
<%@page import="db.Backreq"%>
<%@page import="db.BackreqDAO"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	boolean success = false;
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	BackmatDAO backmatDAO = (BackmatDAO)ctx.getBean("BackmatDAO");
	OutreqDAO outreqDAO = (OutreqDAO)ctx.getBean("OutreqDAO");
	BormatDAO bormatDAO = (BormatDAO)ctx.getBean("BormatDAO");
	ProjectDAO projectDAO = (ProjectDAO)ctx.getBean("ProjectDAO");
	BackreqDAO backreqDAO = (BackreqDAO)ctx.getBean("BackreqDAO");
	SupplierDAO supplierDAO = (SupplierDAO)ctx.getBean("SupplierDAO");
	UserDAO userDAO = (UserDAO)ctx.getBean("UserDAO");
	ReceivepurmatDAO receivepurmatDAO = (ReceivepurmatDAO)ctx.getBean("ReceivepurmatDAO");
	RetreqDAO retreqDAO = (RetreqDAO)ctx.getBean("RetreqDAO");
	MaterialDAO materialDAO = (MaterialDAO)ctx.getBean("MaterialDAO");
	OutpurmatDAO outpurmatDAO = (OutpurmatDAO)ctx.getBean("OutpurmatDAO");
	RetmatDAO retmatDAO = (RetmatDAO)ctx.getBean("RetmatDAO");
	TeamDAO teamDAO = (TeamDAO)ctx.getBean("TeamDAO");
	ToreqDAO toreqDAO = (ToreqDAO)ctx.getBean("ToreqDAO");
	TomatDAO tomatDAO = (TomatDAO)ctx.getBean("TomatDAO");
	
	
	
	String pid = request.getParameter("pid");
	String[] matidtype = request.getParameterValues("matidtype");
	String[] matidvalue = request.getParameterValues("matidvalue");
	String[] matnum = request.getParameterValues("shenpi");
	int userid = (Integer)session.getAttribute("id");
	List list =new ArrayList();
	String message = "";
	
	Toreq toreq = toreqDAO.findById(Integer.parseInt(pid));
	
	//因为是借转出。成功后不涉及改动总库存表
				//取得借库申请相关字段加入，出库申请表中。
			Outreq outreq = new Outreq();
			Borreq borreq=toreq.getBorreq();
			if(borreq!=null && borreq.getCompanyId()!=null){
				outreq.setCompanyId(borreq.getCompanyId());
				outreq.setCompany(borreq.getCompany());
			}else{
				outreq.setCompanyId(0);
				outreq.setCompany(null);
			}
			Date date = new Date();
			outreq.setAssReqDate(date);
			outreq.setAssReqState(2);
			outreq.setAssReqUserId(userid);
			outreq.setDate(date);
			outreq.setMaReqDate(date);
			outreq.setMaReqState(2);
			outreq.setMaReqUserId(userid);
			outreq.setOutCode("O" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));//临时生成出库申请单编号O+年月日
			outreq.setOutReqDate(date);
			outreq.setOutReqState(2);
			outreq.setOutReqUserId(userid);
			outreq.setReqUserId(userid);
			outreq.setPhase(7);//借转出
			if(borreq!=null && borreq.getProId()!=null){
				outreq.setProId(borreq.getProId());
				outreq.setProject(borreq.getProject());
			}else{
				outreq.setProId(0);
				outreq.setProject(null);
			}
			if(borreq!=null && borreq.getReccompanyId()!=null){
				outreq.setReccompany(borreq.getReccompany());
				outreq.setReccompanyId(borreq.getReccompanyId());
			}else{
				outreq.setReccompany(null);
				outreq.setReccompanyId(0);
			}
			outreq.setRemark(borreq.getRemark());
			outreq.setReqDate(date);
			if(borreq!=null && borreq.getReqUser()!=null){
				outreq.setReqUser(borreq.getReqUser());
			}else{
				outreq.setReqUser(null);
			}
			outreq.setReqUserDate(borreq.getReqUserDate());
			outreq.setTeam(borreq.getTeam());
			outreq.setTeamId(borreq.getTeamId());
			outreq.setTob(borreq.getTob());
			outreq.setTobId(borreq.getTobId());
			outreqDAO.save(outreq);
			//得到outreqid
			int oid = outreq.getId();
			//更新出库申请单编号
			outreq.setOutCode("O" + oid);//临时生成出库申请单编号O+年月日
			outreqDAO.attachDirty(outreq);
	
	if(matidtype.length>0 && pid!=null){//确认有勾选物资
		for(int i=0;i<matidtype.length;i++){
			if(matidtype[i]=="1" || "1".equals(matidtype[i])){//有效
				//Retmat retmat = retmatDAO.findById(Integer.parseInt(matidvalue[i]));
				//Backmat	backmat = backmatDAO.findById(Integer.parseInt(matidvalue[i]));
				
				Tomat tomat=tomatDAO.findById(Integer.parseInt(matidvalue[i]));
				Bormat bormat=tomat.getBormat();
				
				tomat.setState(3);//正是出库
				//转换数量
				int matnumnew = Integer.parseInt(matnum[i]);
				tomat.setOkNum(matnumnew);
				
				int duoyu = tomat.getReqNum();//原始申请数量
				int zhengshi = Integer.parseInt(matnum[i]);//最终出库数量
				int addnum = duoyu - zhengshi;
				int un = bormat.getBackUseNum();//出库可用数量
				int retn = bormat.getBackNum();
				
				bormat.setBackUseNum(un+addnum);
				bormat.setBackNum(retn-zhengshi);//正是退库数量
				
				//产生新的出库物资表数据。
				Outpurmat outpurmat = new Outpurmat();
				outpurmat.setDate(new Date());
				outpurmat.setReceivePurmatId(bormat.getReceivePurmatId());
				outpurmat.setMaReqNum(matnumnew);
				outpurmat.setOutReqId(oid);
				outpurmat.setOutreq(outreq);
				outpurmat.setOutReqNum(matnumnew);
				outpurmat.setReqNum(matnumnew);
				outpurmat.setRetNum(matnumnew);
				outpurmat.setRetUseNum(matnumnew);
				outpurmat.setState(3);//正式出库
				outpurmat.setTaskCode(bormat.getTaskCode());
				outpurmatDAO.save(outpurmat);
				tomat.setOutpurmatId(outpurmat.getId());
				bormatDAO.attachDirty(bormat);
				tomatDAO.attachDirty(tomat);
			}else{
				//没有勾选归还可退数
				//Retmat retmat = retmatDAO.findById(Integer.parseInt(matidvalue[i]));
				//Outpurmat outpurmat = retmat.getOutpurmat();
				
				Tomat tomat=tomatDAO.findById(Integer.parseInt(matidvalue[i]));
				Bormat bormat=tomat.getBormat();
				
				int duoyu = tomat.getReqNum();
				int un = bormat.getBackUseNum();//.getRetUseNum();
				bormat.setBackUseNum(un+duoyu);
				tomat.setState(2);//无效状态
				bormatDAO.attachDirty(bormat);
				tomatDAO.attachDirty(tomat);
				
			}
		}
		
		//更新审批状态
		//Retreq retreq = retreqDAO.findById(Integer.parseInt(pid));
		
		
		
		
		toreq.setOutreqId(oid);
		toreq.setPhase(5);//分配流程结束
		toreq.setOkDate(new Date());
		toreq.setOkState(2);//审批通过
		toreq.setOkUserId(userid);
		toreqDAO.attachDirty(toreq);
		success = true;
		message = "恭喜您！借转出出库确认成功！";
	}else{
		message = "借转出出库确认失败！";
		success = false;
	}
	request.setAttribute("success",success);
	request.setAttribute("message",message);

	request.setAttribute("link","借转出出库确认");
	request.setAttribute("url","/jsp/borrowStock/borrowByOutOk/borrowByOutOk_list.jsp");
	pageContext.forward("/vm/borrowStock/message.vm");
%>
<%!
	public List getListReMap(List list){
		List listmap = new ArrayList();
		//System.out.println("===============>>>>>>>>");
		if(list != null && list.size()>0){
		
			for(int i=0;i<list.size();i++){
			
				Map map = new HashMap();
				Object[] aa= (Object[]) list.get(i);
				map.put("pid", aa[0]);
				map.put("pstate", aa[1]);
				map.put("pcode", aa[2]);
				map.put("procode", aa[3]);
				map.put("proname", aa[4]);
				SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd"); 
				if(aa[5]!=null){
					map.put("pdate", sf.format(aa[5]));
				}
				
				if(aa[6]!=null){
					map.put("penddate", sf.format(aa[6]));
				}
				
				listmap.add(map);
			}
		}
		return listmap;
	}
	
	public List getListReMap2(List list,MaterialDAO materialDAO){
		List listmap = new ArrayList();
		if(list != null && list.size()>0){
			for(int i=0;i<list.size();i++){
				Map map = new HashMap();
				Purmat purmat = new Purmat();
				purmat = (Purmat)list.get(i);
				if(purmat!=null){
					map.put("purid", purmat.getId());//
					map.put("purnum", purmat.getNumber());//申购数量
					map.put("purprice",purmat.getPrice());//申购价格
					//根据物质id关联物质编码和名称
					if(purmat.getMaterialId()!=null){
						int mid = purmat.getMaterialId();
						Material material = new Material();
						material = materialDAO.findById(mid);
						map.put("matcode", material.getCode());
						map.put("matname", material.getName());
						map.put("matcom", material.getCompany());
					}
				}
				listmap.add(map);
			}
		}
		return listmap;
	}
%>